using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using CStock;
using CIndicators;


namespace CTrading
{
	/// <summary>
	/// Summary description for ConditionTradeSystem.
	/// </summary>
	public class ConditionTradeSystem : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.ComboBox comboBoxIndicatorName;
		private System.Windows.Forms.ComboBox comboBoxPeriod;
		private System.Windows.Forms.ComboBox comboBoxBuyOrSell;
		private System.Windows.Forms.ComboBox comboBoxAndOrOr;
		
		private System.Windows.Forms.ComboBox comboBoxOptimizeEnd;
		private System.Windows.Forms.ComboBox comboBoxOptimizeStart;
		private System.Windows.Forms.ComboBox comboBoxThresholdValueOrIndicator;
		private System.Windows.Forms.ComboBox comboBoxGreaterOrLess;
		private System.Windows.Forms.ComboBox comboBoxOptimizeStep;

		private int IndexCondition;

		private TradeCondition tradeCondition;
		
		private System.Windows.Forms.CheckBox checkBoxIsIndicator;
		private System.Windows.Forms.CheckBox checkBoxOneDayLess;
		private System.Windows.Forms.ComboBox comboBoxPeriodIndicator2;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

	
		#region events

		public event ConditionChangedEventHandler ConditionChanged;

		#endregion

		public ConditionTradeSystem(int MyIndexCondition,TradeCondition MytradeCondition)
		{
			IndexCondition = MyIndexCondition;
			tradeCondition = MytradeCondition;
			
			InitializeComponent();

			InitializeCustom();

			
			// TODO: Add any initialization after the InitForm call

		}
		private void InitializeCustom()
		{

			if(tradeCondition == null)
			{
				tradeCondition = new TradeCondition();
				//InitializeDefaultTradeCondition();
				
			}

			PopulateControls();
			SetControlSelections();
			//RaiseChangedEvent
		}
		private void PopulateControls()
		{

			comboBoxThresholdValueOrIndicator.Items.Add("0");
			comboBoxOptimizeStart.Items.Add("0");
			comboBoxOptimizeEnd.Items.Add("20");
			comboBoxOptimizeStep.Items.Add("5");



			foreach(string str in IndEvaluator.IndicatorsAvailableNames)
			{
				this.comboBoxIndicatorName.Items.Add(str);
				this.comboBoxThresholdValueOrIndicator.Items.Add(str);

			}
			

			
			
		}


		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void RaiseChangedEvent()
		{
			
			if( null != ConditionChanged )
			{

				ConditionChanged(this,new ConditionChangedEvent(this.IndexCondition, this.tradeCondition));
			}
			
		}
		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.comboBoxBuyOrSell = new System.Windows.Forms.ComboBox();
			this.comboBoxAndOrOr = new System.Windows.Forms.ComboBox();
			this.comboBoxIndicatorName = new System.Windows.Forms.ComboBox();
			this.comboBoxOptimizeStep = new System.Windows.Forms.ComboBox();
			this.comboBoxOptimizeEnd = new System.Windows.Forms.ComboBox();
			this.comboBoxOptimizeStart = new System.Windows.Forms.ComboBox();
			this.comboBoxThresholdValueOrIndicator = new System.Windows.Forms.ComboBox();
			this.comboBoxGreaterOrLess = new System.Windows.Forms.ComboBox();
			this.comboBoxPeriod = new System.Windows.Forms.ComboBox();
			this.checkBoxIsIndicator = new System.Windows.Forms.CheckBox();
			this.checkBoxOneDayLess = new System.Windows.Forms.CheckBox();
			this.comboBoxPeriodIndicator2 = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// comboBoxBuyOrSell
			// 
			this.comboBoxBuyOrSell.Items.AddRange(new object[] {
																   "Buy",
																   "Sell"});
			this.comboBoxBuyOrSell.Location = new System.Drawing.Point(576, 0);
			this.comboBoxBuyOrSell.Name = "comboBoxBuyOrSell";
			this.comboBoxBuyOrSell.Size = new System.Drawing.Size(56, 21);
			this.comboBoxBuyOrSell.TabIndex = 5;
			// 
			// comboBoxAndOrOr
			// 
			this.comboBoxAndOrOr.Items.AddRange(new object[] {
																 "And",
																 "Or"});
			this.comboBoxAndOrOr.Location = new System.Drawing.Point(648, 0);
			this.comboBoxAndOrOr.Name = "comboBoxAndOrOr";
			this.comboBoxAndOrOr.Size = new System.Drawing.Size(56, 21);
			this.comboBoxAndOrOr.TabIndex = 6;
			// 
			// comboBoxIndicatorName
			// 
			this.comboBoxIndicatorName.Location = new System.Drawing.Point(8, 0);
			this.comboBoxIndicatorName.Name = "comboBoxIndicatorName";
			this.comboBoxIndicatorName.Size = new System.Drawing.Size(224, 21);
			this.comboBoxIndicatorName.TabIndex = 1;
			// 
			// comboBoxOptimizeStep
			// 
			this.comboBoxOptimizeStep.Location = new System.Drawing.Point(848, 0);
			this.comboBoxOptimizeStep.Name = "comboBoxOptimizeStep";
			this.comboBoxOptimizeStep.Size = new System.Drawing.Size(56, 21);
			this.comboBoxOptimizeStep.TabIndex = 9;
			// 
			// comboBoxOptimizeEnd
			// 
			this.comboBoxOptimizeEnd.Location = new System.Drawing.Point(784, 0);
			this.comboBoxOptimizeEnd.Name = "comboBoxOptimizeEnd";
			this.comboBoxOptimizeEnd.Size = new System.Drawing.Size(56, 21);
			this.comboBoxOptimizeEnd.TabIndex = 8;
			// 
			// comboBoxOptimizeStart
			// 
			this.comboBoxOptimizeStart.Location = new System.Drawing.Point(712, 0);
			this.comboBoxOptimizeStart.Name = "comboBoxOptimizeStart";
			this.comboBoxOptimizeStart.Size = new System.Drawing.Size(56, 21);
			this.comboBoxOptimizeStart.TabIndex = 7;
			// 
			// comboBoxThresholdValueOrIndicator
			// 
			this.comboBoxThresholdValueOrIndicator.Location = new System.Drawing.Point(352, 0);
			this.comboBoxThresholdValueOrIndicator.Name = "comboBoxThresholdValueOrIndicator";
			this.comboBoxThresholdValueOrIndicator.Size = new System.Drawing.Size(160, 21);
			this.comboBoxThresholdValueOrIndicator.TabIndex = 4;
			// 
			// comboBoxGreaterOrLess
			// 
			this.comboBoxGreaterOrLess.Items.AddRange(new object[] {
																	   ">",
																	   "=",
																	   "<",
																	   ">=",
																	   "<="});
			this.comboBoxGreaterOrLess.Location = new System.Drawing.Point(296, 0);
			this.comboBoxGreaterOrLess.Name = "comboBoxGreaterOrLess";
			this.comboBoxGreaterOrLess.Size = new System.Drawing.Size(48, 21);
			this.comboBoxGreaterOrLess.TabIndex = 3;
			// 
			// comboBoxPeriod
			// 
			this.comboBoxPeriod.Location = new System.Drawing.Point(240, 0);
			this.comboBoxPeriod.Name = "comboBoxPeriod";
			this.comboBoxPeriod.Size = new System.Drawing.Size(48, 21);
			this.comboBoxPeriod.TabIndex = 2;
			// 
			// checkBoxIsIndicator
			// 
			this.checkBoxIsIndicator.Location = new System.Drawing.Point(528, 0);
			this.checkBoxIsIndicator.Name = "checkBoxIsIndicator";
			this.checkBoxIsIndicator.Size = new System.Drawing.Size(24, 16);
			this.checkBoxIsIndicator.TabIndex = 10;
			// 
			// checkBoxOneDayLess
			// 
			this.checkBoxOneDayLess.Location = new System.Drawing.Point(984, 0);
			this.checkBoxOneDayLess.Name = "checkBoxOneDayLess";
			this.checkBoxOneDayLess.Size = new System.Drawing.Size(32, 16);
			this.checkBoxOneDayLess.TabIndex = 11;
			// 
			// comboBoxPeriodIndicator2
			// 
			this.comboBoxPeriodIndicator2.Location = new System.Drawing.Point(912, 2);
			this.comboBoxPeriodIndicator2.Name = "comboBoxPeriodIndicator2";
			this.comboBoxPeriodIndicator2.Size = new System.Drawing.Size(48, 21);
			this.comboBoxPeriodIndicator2.TabIndex = 12;
			// 
			// ConditionTradeSystem
			// 
			this.Controls.Add(this.comboBoxPeriodIndicator2);
			this.Controls.Add(this.checkBoxOneDayLess);
			this.Controls.Add(this.checkBoxIsIndicator);
			this.Controls.Add(this.comboBoxPeriod);
			this.Controls.Add(this.comboBoxGreaterOrLess);
			this.Controls.Add(this.comboBoxThresholdValueOrIndicator);
			this.Controls.Add(this.comboBoxOptimizeStart);
			this.Controls.Add(this.comboBoxOptimizeEnd);
			this.Controls.Add(this.comboBoxOptimizeStep);
			this.Controls.Add(this.comboBoxIndicatorName);
			this.Controls.Add(this.comboBoxAndOrOr);
			this.Controls.Add(this.comboBoxBuyOrSell);
			this.Name = "ConditionTradeSystem";
			this.Size = new System.Drawing.Size(1024, 24);
			this.ResumeLayout(false);

		}
		#endregion

//		private void comboBoxBuyOrSell_SelectedIndexChanged(object sender, System.EventArgs e)
//		{
//			this.tradeCondition.BuyOrSell = comboBoxBuyOrSell.SelectedItem.ToString();
//			RaiseChangedEvent();
//		}
		public TradeCondition TradeCondition
		{
			get
			{
				GetTradeConditionFromControlSelections();
				return this.tradeCondition;
			}
			set
			{
				this.tradeCondition = value;
			}
		}

		private void SetControlSelections()
		{
			this.comboBoxAndOrOr.SelectedItem = tradeCondition.AndOrOr;
			this.comboBoxBuyOrSell.SelectedItem = tradeCondition.BuyOrSell;
			this.comboBoxGreaterOrLess.SelectedItem = tradeCondition.GreaterOrLess;
			
			if(! this.comboBoxPeriod.Items.Contains( tradeCondition.Period.ToString()))
			{
				this.comboBoxPeriod.Items.Add( tradeCondition.Period.ToString());
			}
			if(! this.comboBoxPeriodIndicator2.Items.Contains( tradeCondition.PeriodIndicator2.ToString()))
			{
				this.comboBoxPeriodIndicator2.Items.Add( tradeCondition.PeriodIndicator2.ToString());
			}
			

			this.comboBoxPeriodIndicator2.SelectedItem = tradeCondition.PeriodIndicator2.ToString();
			this.comboBoxPeriod.SelectedItem = tradeCondition.Period.ToString();
			if(tradeCondition.IndicatorName == null)
			{
				tradeCondition.IndicatorName = this.comboBoxIndicatorName.Items[0].ToString();
				
			}
			else
			{
				this.comboBoxIndicatorName.SelectedItem = tradeCondition.IndicatorName;
			}
		
			this.checkBoxIsIndicator.Checked = tradeCondition.ThresholdIsIndicator;
						
			
			if(! this.comboBoxIndicatorName.Items.Contains(tradeCondition.IndicatorName))
			{
				this.comboBoxIndicatorName.Items.Add(tradeCondition.IndicatorName);
			}
			
			
			if(tradeCondition.ThresholdValueOrIndicator == null)
			{
				tradeCondition.ThresholdValueOrIndicator = "0";			
			}
		
			if(! this.comboBoxThresholdValueOrIndicator.Items.Contains(tradeCondition.ThresholdValueOrIndicator))
			{
				this.comboBoxThresholdValueOrIndicator.Items.Add(tradeCondition.ThresholdValueOrIndicator);
			}

			this.comboBoxThresholdValueOrIndicator.SelectedItem = tradeCondition.ThresholdValueOrIndicator;
			
			if(! this.comboBoxOptimizeStart.Items.Contains(tradeCondition.OptimizeStart.ToString()))
			{
				this.comboBoxOptimizeStart.Items.Add(tradeCondition.OptimizeStart.ToString());
			}
			this.comboBoxOptimizeStart.SelectedItem = tradeCondition.OptimizeStart.ToString();

			if(! this.comboBoxOptimizeEnd.Items.Contains(tradeCondition.OptimizeEnd.ToString()))
			{
				this.comboBoxOptimizeEnd.Items.Add(tradeCondition.OptimizeEnd.ToString());
			}
			this.comboBoxOptimizeEnd.SelectedItem = tradeCondition.OptimizeEnd.ToString();

			if(! this.comboBoxOptimizeStep.Items.Contains( tradeCondition.OptimizeStep.ToString()))
			{
				this.comboBoxOptimizeStep.Items.Add( tradeCondition.OptimizeStep.ToString());
			}
			this.comboBoxOptimizeStep.SelectedItem = tradeCondition.OptimizeStep.ToString();


		}
		private void GetTradeConditionFromControlSelections()
		{
			try
			{
				tradeCondition.AndOrOr = this.comboBoxAndOrOr.Text ;
				tradeCondition.BuyOrSell = this.comboBoxBuyOrSell.Text ;
				tradeCondition.GreaterOrLess = this.comboBoxGreaterOrLess.Text;
				tradeCondition.IndicatorName = this.comboBoxIndicatorName.Text;
				tradeCondition.ThresholdIsIndicator = this.checkBoxIsIndicator.Checked;



			
				tradeCondition.ThresholdValueOrIndicator = this.comboBoxThresholdValueOrIndicator.Text;
		

				try
				{
					tradeCondition.Period = Convert.ToInt32(this.comboBoxPeriod.Text);
				}
				catch
				{
					tradeCondition.Period = 4;
				}
				try
				{
					tradeCondition.PeriodIndicator2 = Convert.ToInt32(this.comboBoxPeriodIndicator2.Text);
				}
				catch
				{
					tradeCondition.PeriodIndicator2 = 4;
				}

				

				try
				{
					tradeCondition.OptimizeStart = Convert.ToDouble(this.comboBoxOptimizeStart.Text);
				}
				catch
				{
					tradeCondition.OptimizeStart = Convert.ToDouble(0);
			
				}
				try
				{
					tradeCondition.OptimizeEnd = Convert.ToDouble(this.comboBoxOptimizeEnd.Text);
				}
				catch
				{
					tradeCondition.OptimizeEnd = Convert.ToDouble(20);
			
				}
				try
				{
					tradeCondition.OptimizeStep = Convert.ToDouble(comboBoxOptimizeStep.Text);
				}
				catch
				{
					tradeCondition.OptimizeStep = Convert.ToDouble(5);
			
				}
			}
			catch(Exception e)
			{
				System.Diagnostics.Debug.Assert(false,"ConditionTradeSystem: could not get data from combo boxes \n" + e.Message);

			}

		


		}


	}
}
